package org.shopen.ai.puremvc.model.problem.cities
{
	import org.shopen.ai.puremvc.model.problem.IAction;
	import org.shopen.ai.puremvc.model.problem.ICostFunction;
	import org.shopen.ai.puremvc.model.problem.IState;
	import org.shopen.ai.puremvc.model.utils.Convert;
	import org.un.cava.birdeye.ravis.graphLayout.data.IGraph;
	import org.un.cava.birdeye.ravis.graphLayout.data.INode;
	
	public class CostFunction implements ICostFunction 
	{
		// holds reference to model G(V,E)
		protected var graph : IGraph;
		
		public function CostFunction(graph : IGraph)
		{
			this.graph = graph;
		}
		
		public function getCost(state : IState, action : IAction) : Number
		{
			var result : Number = 0;
			
			var node1 : INode = graph.nodeById((state as INode).id);
			var node2 : INode = graph.nodeById((action.act() as INode).id);
			
			result = Convert.toNumber(graph.getEdge(node1, node2).data.@distance);
			
			return result;
		}
		
		public function costToString(value : Number) : String
		{
			var result : String = value.toString() + " km";
			return result;
		}

	}
}